﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Camc.Duct.TaskProcess.Dto;
using Camc.Duct.TaskProcess.Process;
using Camc.Duct.TaskProcess.Station;
using Camc.Duct.TaskProcess.TaskLine;
using System.Linq.Dynamic.Core;
using Camc.Duct.TaskProcess.Printer;

namespace Camc.Duct.TaskProcess
{
	public class TemplateAppService:DuctAppServiceBase
	{
		private readonly IRepository<NodeBase, long> _nodeBaseRepository;
		private readonly IRepository<NodeForTemplate, long> _nodeForTemplateRepository;
		private readonly IRepository<LineTemplate, Guid> _lineTemplateRepository;
		private readonly IRepository<WorkStation, Guid> _workStationRepository;
		private readonly IRepository<NodeForInstance, long> _nodeForInstanceRepository;
		private readonly IRepository<LineInstance, Guid> _lineInstanceRepository;

		public TemplateAppService(IRepository<NodeBase, long> nodeBaseRepository, IRepository<LineTemplate, Guid> lineTemplateBaseRepository, IRepository<WorkStation, Guid> workStationRepository, IRepository<NodeForTemplate, long> nodeRepository, IRepository<LineInstance, Guid> lineInstanceRepository, IRepository<NodeForInstance, long> nodeForInstanceRepository)
		{
			_nodeBaseRepository = nodeBaseRepository;
			_lineTemplateRepository = lineTemplateBaseRepository;
			_workStationRepository = workStationRepository;
			_nodeForTemplateRepository = nodeRepository;
			_lineInstanceRepository = lineInstanceRepository;
			_nodeForInstanceRepository = nodeForInstanceRepository;
		}



		#region 工位管理

		public List<WorkStationUpdateOrOutput> GetAllStations()
		{
			return ObjectMapper.Map<List<WorkStationUpdateOrOutput>>(_workStationRepository.GetAll().ToList()) ;
		}
		public Guid CreateWorkStation(WorkStationDto input)
		{
			return _workStationRepository.InsertAndGetId(ObjectMapper.Map<WorkStation>(input));
		}

		public void UpdateWorkStation(WorkStationUpdateOrOutput input)
		{
			var workStation = _workStationRepository.Get(input.Id);

			workStation.StationName = input.StationName;

			 _workStationRepository.Update(workStation);
		}

		public void DeleteWorkStation(Guid workStationGuid)
		{
			var workStation = _workStationRepository.Get(workStationGuid);
			_workStationRepository.Delete(workStation);
		}

		public void PrintStationQr(string qrCode,string stationName)
		{
			LabelPrint.Print(qrCode.ToString(), "", "", stationName);
		}

		#endregion

		#region 工序库管理（暂时无用）

		public List<NodeBaseUpdateOrOutput> GetAllNodeBase()
		{
			return ObjectMapper.Map<List<NodeBaseUpdateOrOutput>>(_nodeBaseRepository.GetAll().ToList()); 
		
		}
		public long CreateNodeBase(NodeBaseDto input)
		{
			return _nodeBaseRepository.InsertAndGetId(ObjectMapper.Map<NodeBase>(input));
		}


		public void UpdateNodeBase(NodeBaseUpdateOrOutput input)
		{
			var nodeBase = _nodeBaseRepository.Get(input.Id);

			nodeBase.NodeCode = input.NodeCode;
			nodeBase.NodeName = input.NodeName;
			nodeBase.WorkStationId = input.WorkStationId;

			_nodeBaseRepository.Update(nodeBase);
		}

		public void DeleteNodeBase(long id)
		{
			var nodeBase = _nodeBaseRepository.Get(id);
			_nodeBaseRepository.Delete(nodeBase);
		}


		#endregion

		#region 执行模板管理

		public List<LineTemplateUpdate> GetAllLineTemplate()
		{
			var lineTmps = _lineTemplateRepository.GetAllIncluding(c => c.TemplateNodeList)
				.Where(c => c.IsDeleted == false).ToList();

			var output = ObjectMapper.Map<List<LineTemplateUpdate>>(lineTmps);

			return output;
		}
		public void DeleteLineTemplate(Guid id)
		{
			var lineTmp = _lineTemplateRepository.Get(id);

			_lineTemplateRepository.Delete(lineTmp);
		}

		public void CreateOrUpdateLineTemplateNodeList(LineTemplateUpdate input)
		{
			LineTemplate lineTemplate;
			if (!input.Id.IsNullOrWhiteSpace())
			{
				lineTemplate = _lineTemplateRepository.GetAllIncluding(c=>c.TemplateNodeList).FirstOrDefault(c => c.Id == new Guid(input.Id));
				if (lineTemplate != null)
				{
					lineTemplate.TemplateName = input.TemplateName;
					lineTemplate.TemplateNodeList = null;
				}
			}
			else
			{
				lineTemplate = _lineTemplateRepository.Insert(new LineTemplate(input.TemplateName));
			}


			if (input.TemplateNodeList != null)
				foreach (var node in input.TemplateNodeList)
				{
					if (lineTemplate != null)
					{
						var nodeForTmp = new NodeForTemplate()
						{
							LineTemplateId = lineTemplate.Id,
							NodeCode = node.NodeCode,
							NodeName = node.NodeName,
							OrderIndex = node.OrderIndex,
							WorkStationId = node.WorkStationId
						};

						_nodeForTemplateRepository.Insert(nodeForTmp);
					}
				}
		}

		#endregion

		

	
	}
}
